package com.gavin.demo4fresco.module.navigation;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.gavin.demo4fresco.R;
import com.gavin.demo4fresco.config.GlobalVars;

public class RoundDemoActivity extends AppCompatActivity {
    /**
     * 原 xml 配置的效果展示 View
     */
    private SimpleDraweeView mXmlView;
    /**
     * 代码动态绘制圆角效果
     */
    private SimpleDraweeView mCircleView;
    /**
     * 代码绘制圆形效果
     */
    private SimpleDraweeView mRoundCornerView;
    private SimpleDraweeView mProgressiveJpegView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_round_demo);

        mXmlView = (SimpleDraweeView) findViewById(R.id.round_view_1);
        mXmlView.setImageURI(Uri.parse(GlobalVars.URLS[0]));
        mCircleView = (SimpleDraweeView) findViewById(R.id.round_view_2);
        mCircleView.setImageURI(Uri.parse(GlobalVars.URLS[0]));
        mRoundCornerView = (SimpleDraweeView) findViewById(R.id.round_view_3);
        mRoundCornerView.setImageURI(Uri.parse(GlobalVars.URLS[0]));
        mProgressiveJpegView = (SimpleDraweeView) findViewById(R.id.progressive_jpeg);

        RoundingParams roundingParams = mCircleView.getHierarchy().getRoundingParams();
        roundingParams.setBorder(R.color.colorAccent,5.0f).setRoundAsCircle(true);
        mCircleView.getHierarchy().setRoundingParams(roundingParams);

        RoundingParams roundingParams2 = RoundingParams.fromCornersRadius(10f).setOverlayColor(android.R.color.holo_red_dark);
        GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources());
        GenericDraweeHierarchy hierarchy = builder.setFadeDuration(500).setRoundingParams(roundingParams2).build();
        mRoundCornerView.setHierarchy(hierarchy);

        // 目前，我们必须显式地在加载时，允许渐进式JPEG图片加载,fresco 希望在后续的版本中,
        // 在 setImageURI 方法中可以直接支持渐进式图片加载
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(GlobalVars.URLS[GlobalVars.URLS.length - 1]))
                .setProgressiveRenderingEnabled(true)
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(mProgressiveJpegView.getController())
                .build();
        mProgressiveJpegView.getHierarchy().setProgressBarImage(new ProgressBarDrawable());
        mProgressiveJpegView.setController(controller);
    }
}
